<?php

namespace falcon5\inc\Http\Controllers;

use falcon5\inc\Exceptions\RemoteServiceException;
use falcon5\inc\Helpers\UrlHelper;
use falcon5\inc\Http\JsonResponse;
use falcon5\inc\Http\Request;
use falcon5\inc\Infrastructure\Services\Falcon5Service;
use hollisho\helpers\ArrayHelper;
use hollisho\helpers\InvalidArgumentException;
use lucatume\DI52\ContainerException;

/**
 * Class GatewayController
 * @package falcon5\inc\Http\Controllers
 */
class GatewayController extends BaseController
{

    /**
     * save entry
     * @param Request $request
     * @return JsonResponse
     * @throws RemoteServiceException
     * @throws ContainerException|InvalidArgumentException
     * 
     */
    public function saveEntry(Request $request): JsonResponse
    {
        /** @var Falcon5Service $service */
        $service = falcon5_app(Falcon5Service::class);
        $body = $request->all();
        $result = $service->saveEntry($body);
        
        return $this->handleServiceResponse($result);
    }

    /**
     * fetch entry list
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerException
     * @throws RemoteServiceException
     * @throws InvalidArgumentException
     */
    public function entryList(Request $request): JsonResponse
    {
        $pageId = $request->get('page_id');
        $keyword = $request->get('keyword');
        $originalLang = $request->get('original_lang');
        $translatedLang = $request->get('translated_lang');
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 10);

        /** @var Falcon5Service $service */
        $service = falcon5_app(Falcon5Service::class);
        $result = $service->entryList($pageId, $originalLang, $translatedLang, $keyword, $page, $pageSize);

        return $this->handleServiceResponse($result, function($data) {
            return [
                'list' => array_map(function($item) {
                    return [
                        'id' => $item['id'],
                        'original' => $item['original'],
                        'translated' => $item['translated'],
                    ];
                }, array_filter(ArrayHelper::getValue($data, 'list', []))),
                'total' => ArrayHelper::getValue($data, 'count', 0),
                'page' => ArrayHelper::getValue($data, 'page', 0),
                'page_size' => ArrayHelper::getValue($data, 'page_size', 0),
            ];
        });
    }

    /**
     * validate api key
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerException
     * @throws InvalidArgumentException
     * @throws RemoteServiceException
     */
    public function validateKey(Request $request): JsonResponse
    {
        /** @var Falcon5Service $service */
        $service = falcon5_app(Falcon5Service::class);
        $domain = UrlHelper::getHost();
        $body = array_merge($request->all(), [
            'domain' => $domain
        ]);
        
        $result = $service->validateKey($body);
        
        return $this->handleServiceResponse($result);
    }

    /**
     * fetch url slug
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerException
     * @throws RemoteServiceException|InvalidArgumentException
     */
    public function translateUrl(Request $request): JsonResponse
    {
        $url = $request->get('original_path');
        $translatedLang = $request->get('translated_lang');

        /** @var Falcon5Service $service */
        $service = falcon5_app(Falcon5Service::class);
        $result = $service->translateUrl($url, $translatedLang);
        
        return $this->handleServiceResponse($result);
    }

}